package com.golden.order02.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.dao.OrderProductItemMapper;
import com.golden.order02.domain.OrderProductItemDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.entity.OrderProductItem;
import com.golden.order02.query.OrderProductItemExample;

@Service
public class OrderProductItemServiceImpl implements OrderProductItemService{
	
	@Autowired
	OrderProductItemMapper orderProductItemMapper;
	
	public ResultDO<OrderProductItemDO> get( Integer id){
		ResultDO<OrderProductItemDO> result=new ResultDO<OrderProductItemDO>();
		BaseServiceImpl<OrderProductItemDO,OrderProductItem> bi=new BaseServiceImpl<>();
		OrderProductItemDO oo=new OrderProductItemDO();
		
		try {
			OrderProductItem selectByPrimaryKey = orderProductItemMapper.selectByPrimaryKey(id);
			OrderProductItemDO t = bi.getT(selectByPrimaryKey, oo);
			result.setData(t);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<List<OrderProductItemDO>> query( OrderProductItemExample example){
		ResultDO<List<OrderProductItemDO>> result=new ResultDO<List<OrderProductItemDO>>();
		BaseServiceImpl<OrderProductItemDO,OrderProductItem> bi=new BaseServiceImpl<>();
		OrderProductItemDO oo=new OrderProductItemDO();
		try {
			List<OrderProductItem> selectByExample = orderProductItemMapper.selectByExample(example);
			List<OrderProductItemDO> tList = bi.getTList(selectByExample, oo);
			result.setData(tList);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result; 
	}
	
	public ResultDO<OrderProductItemDO> add( OrderProductItemDO beanDO){
		ResultDO<OrderProductItemDO> result=new ResultDO<OrderProductItemDO>();
		BaseServiceImpl<OrderProductItem, OrderProductItemDO> bi=new BaseServiceImpl<>();
		OrderProductItem oi=new OrderProductItem();
		OrderProductItem t = bi.getT(beanDO, oi);
		try {
			t.setCreateTime(new Date());
			t.setModifiTime(new Date());
			int insert = orderProductItemMapper.insert(t);
			if(insert<=0) {
				throw new Exception();
			}
			beanDO.setId(t.getId());
			result.setData(beanDO);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<Integer> delete( Integer id){
		ResultDO<Integer> result=new ResultDO<Integer>(); 
		try {
			int deleteByPrimaryKey = orderProductItemMapper.deleteByPrimaryKey(id);
			if(deleteByPrimaryKey<=0) {
				throw new Exception();
			}
			result.setData(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderProductItemDO> modify( OrderProductItemDO beanDO){
		ResultDO<OrderProductItemDO> result=new ResultDO<OrderProductItemDO>();
		BaseServiceImpl<OrderProductItem, OrderProductItemDO> bi=new BaseServiceImpl<>();
		OrderProductItem oi=new OrderProductItem();
		OrderProductItem t = bi.getT(beanDO, oi);
		try {
			int insert = orderProductItemMapper.updateByPrimaryKeySelective(t);
			if(insert<=0) {
				throw new Exception();
			}
			result.setData(beanDO);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
		}
		return result;
	}
}
